package nuc.edu.cn.controller;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;


@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request){
        String header = request.getHeader("Authorization");
        String token = header.substring(header.indexOf("bearer") + 7);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Claims body = Jwts.parser()
                .setSigningKey("test.key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
        return principal;
    }

}
